package com.aiju.cash.widgit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.aiju.cash.R;
import com.aiju.common.PixValue;

/**
 * @author: 佐为
 * @date: 2016-11-24 14:25
 * @version: V1.0
 * @Description: 带线的输入框
 */
public class LineEditText extends EditText {

    private static final int DRAWABLE_LEFT = 0;
    private static final int DRAWABLE_TOP = 1;
    private static final int DRAWABLE_RIGHT = 2;
    private static final int DRAWABLE_BOTTOM = 3;

    private Drawable mClearDrawable;

    /**
     * 线的高度
     */
    private int height = PixValue.dp.valueOf(0.5f);

    // 画笔 用来画下划线
    private Paint paint;

    private Rect tempRect = new Rect();

    //线的颜色值
    private int lineColor = getResources().getColor(R.color.color_999999);

    public LineEditText(Context context) {
        super(context);
        init();
    }

    public LineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        tempRect.setEmpty();
        tempRect.set(0, getHeight() - height, getWidth(), getHeight());

        paint.setColor(lineColor);
        canvas.drawRect(tempRect, paint);
    }

    private void init() {
        paint = new Paint();
        mClearDrawable = getResources().getDrawable(R.mipmap.icon_edit_delete);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        setClearIconVisible(hasFocus() && text.length() > 0);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        setClearIconVisible(focused && length() > 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                Drawable drawable = getCompoundDrawables()[DRAWABLE_RIGHT];
                if (drawable != null && event.getX() <= (getWidth() - getPaddingRight())
                        && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {
                    setText("");
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    private void setClearIconVisible(boolean visible) {
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[DRAWABLE_LEFT], getCompoundDrawables()[DRAWABLE_TOP],
                visible ? mClearDrawable : null, getCompoundDrawables()[DRAWABLE_BOTTOM]);
    }

    public int getLineColor() {
        return lineColor;
    }

    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }
}
